﻿using System;
using System.IO.Ports;
using System.Linq;
using System.Windows.Forms;
using LModels;

namespace LClient
{
    public partial class frmLidarSettings : Form
    {

        public LidarSettings ls;

        public frmLidarSettings(LidarSettings lidarSettings)
        {
            InitializeComponent();

            // Get all the SerialPort
            comboBox1.Items.Clear();
            foreach (string s in SerialPort.GetPortNames().OrderBy(x => x).ToArray())
                comboBox1.Items.Add(s);

            comboBox3.DataSource = Enum.GetValues(typeof(Parity));

            if (lidarSettings != null)
            {
                ls = lidarSettings;

                comboBox1.SelectedItem = ls.Port;
                comboBox2.SelectedItem = ls.BaudRate;
                comboBox3.SelectedItem = ls.Parity;
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Parity p;
            Enum.TryParse<Parity>(comboBox3.SelectedValue.ToString(), out p);

            ls = new LidarSettings();
            ls.Port = comboBox1.SelectedItem.ToString();
            int baud;
            
            if(int.TryParse(comboBox2.SelectedItem.ToString(), out baud))
                ls.BaudRate = baud;
            ls.Parity = p;

            DialogResult = DialogResult.OK;
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Get all the SerialPort
            comboBox1.Items.Clear();
            foreach (string s in SerialPort.GetPortNames().OrderBy(x => x).ToArray())
                comboBox1.Items.Add(s);

        }
    }
}
